//
//  WGBDatePickerView.m
//  WGBDatePickerView
//
//  Created by apple on 2018/4/12.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "WGBDatePickerView.h"
#import <WGBCustomAlertPopupView/WGBCustomPopUpView.h>

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface WGBDatePickerView()

@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UIView  *topLine;
@property (nonatomic,strong) UIDatePicker *datePicker;
@property (nonatomic,strong) UIButton *cancelButton;
@property (nonatomic,strong) UIButton *confirmButton;
@property (nonatomic,copy) void (^pickDateBlock)(NSDate *date);
@property (nonatomic,copy) dispatch_block_t  dismissBlock;

@end

@implementation WGBDatePickerView

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenWidth - 30 , 44)];
        _titleLabel.text = @"选择时间";
        _titleLabel.textColor = [UIColor colorWithRed:102/255.0f green:102/255.0f blue:102/255.0f alpha:1.0f];
        _titleLabel.font = [UIFont systemFontOfSize:15];
    }
    return _titleLabel;
}

- (UIView *)topLine{
    if (!_topLine) {
        _topLine = [[UIView alloc] init];
        _topLine.backgroundColor = [UIColor colorWithRed:245/255.0f green:245/255.0f blue:245/255.0f alpha:1.0f];
    }
    return _topLine;
}

- (UIDatePicker *)datePicker{
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth , 216)];
        _datePicker.datePickerMode = UIDatePickerModeDate;//年/月/日
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy/MM/dd";
        _datePicker.minimumDate = [dateFormatter dateFromString:@"2017/05/01"];
        _datePicker.maximumDate = [[NSDate date] dateByAddingDays:365*3];
        [self addSubview: _datePicker];
    }
    return _datePicker ;
}

- (UIButton *)cancelButton{
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"取消"];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize: 15.0f] range:NSMakeRange(0, 2)];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:31.0f/255.0f green:161.0f/255.0f blue:255.0f/255.0f alpha:1.0f] range:NSMakeRange(0, 2)];
        [_cancelButton setAttributedTitle: attributedString forState: UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];
        _cancelButton.layer.borderColor = [UIColor colorWithRed:245/255.0f green:245/255.0f blue:245/255.0f alpha:1.0f].CGColor;
        _cancelButton.layer.borderWidth = 1;
    }
    return _cancelButton;
}

- (UIButton *)confirmButton{
    if (!_confirmButton) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_confirmButton setTitle:@"确认" forState:UIControlStateNormal];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"确认"];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize: 15.0f]  range:NSMakeRange(0, 2)];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:31.0f/255.0f green:161.0f/255.0f blue:255.0f/255.0f alpha:1.0f] range:NSMakeRange(0, 2)];
        [_confirmButton setAttributedTitle: attributedString forState: UIControlStateNormal];
        [_confirmButton addTarget:self action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];
        _confirmButton.layer.borderColor = [UIColor colorWithRed:245/255.0f green:245/255.0f blue:245/255.0f alpha:1.0f].CGColor;
        _confirmButton.layer.borderWidth = 1;
    }
    return _confirmButton;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview: self.titleLabel];
        [self addSubview: self.topLine];
        [self addSubview: self.datePicker];
        [self addSubview: self.cancelButton];
        [self addSubview: self.confirmButton];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(15, 0, kScreenWidth - 30 , 44);
    self.topLine.frame = CGRectMake(0, 43, kScreenWidth , 1);
    self.datePicker.frame =CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), kScreenWidth , 216);
    CGFloat buttonW = (kScreenWidth)/2.0;
    CGFloat buttonH = 50;
    self.cancelButton.frame = CGRectMake(0, CGRectGetMaxY(self.datePicker.frame), buttonW , buttonH);
    self.confirmButton.frame = CGRectMake(buttonW , CGRectGetMaxY(self.datePicker.frame), buttonW , buttonH);
}


- (void)dismiss:(UIButton *)button{
    if ([button.currentTitle isEqualToString:@"确认"]) {
        !self.pickDateBlock? : self.pickDateBlock(self.datePicker.date);
    }else{
        !self.dismissBlock? : self.dismissBlock();
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.01;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


+ (void)showPickerViewWithPickerDateBlock:(void (^)(NSDate *))callback{
    
    UIView *contentView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    contentView.backgroundColor = [UIColor clearColor];
    contentView.userInteractionEnabled = YES;
    
    WGBDatePickerView *picker = [[WGBDatePickerView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScreenWidth, kScreenWidth , kScreenWidth)];
    [contentView addSubview: picker];

    WGBCustomPopUpView *alerView  = [[WGBCustomPopUpView alloc] init];
    alerView.contentView = contentView ;
    alerView.touchDismiss = YES;
    [picker setPickDateBlock:^(NSDate *date) {
        !callback? : callback(date);
        [alerView dismiss];
    }];
    [picker setDismissBlock:^{
        [alerView dismiss];
    }];
    alerView.animationType = WGBAlertAnimationTypeBottom ;
    [alerView show];
}



@end
